package com.assignment.health;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.net.http.NetClient;
import com.user.UserInformation;


public class ChangePasswordActivity extends AppCompatActivity {

    class MessageType {
        public final static int changeReturn = 0x01;
    }

    private Button buttonChange;
    private TextView textOld;
    private TextView textNew;
    private TextView textInfor;

    private Handler handler;

    private void bind() {
        buttonChange = (Button) findViewById(R.id.button2);
        textOld = (TextView) findViewById(R.id.editTextNumber_oldpassword);
        textNew = (TextView) findViewById(R.id.editTextTextPassword_newpassword);
        textInfor = (TextView) findViewById(R.id.textView_changepassword);
    }

    private void setAction() {
        buttonChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String oldPassword = textOld.getText().toString();
                String newPassword = textNew.getText().toString();
                String token = UserInformation.token;

                buttonChange.setEnabled(false);
                textOld.setText("");
                textNew.setText("");
                textInfor.setText("");

                if(newPassword.contentEquals("")) {
                    textInfor.setText(R.string.new_password_empty);
                    buttonChange.setEnabled(true);
                }
                else {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            JsonObject json = NetClient.ChangePassword(token, oldPassword, newPassword);
                            handler.sendMessage(handler.obtainMessage(MessageType.changeReturn, json));
                        }
                    }).start();
                }
            }
        });
    }

    @SuppressLint("HandlerLeak")
    private void setHandler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                textInfor.setText("erg");
                if(msg.what == MessageType.changeReturn) {
                    textInfor.setText("aewrg");
                    JsonObject json = (JsonObject) msg.obj;
                    String status = json.get("status").getAsString();
                    buttonChange.setEnabled(true);
                    if(status.contentEquals("OK")) {
                        textInfor.setText("Successfully!");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        finish();
                    }
                    else if(status.contentEquals("PasswordMismatch")) {
                        textInfor.setText(R.string.old_password_wrong);
                    }
                    else {
                        textInfor.setText("Unknow Error!");
                        textInfor.setText(json.get("message").getAsString());
                    }
                }
            }
        };
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);

        bind();
        setHandler();
        setAction();

    }
}